<?php
require APPPATH.'libraries/ApiController.php';
class timecard extends ApiController {
	public function __construct()
	{
		parent::__construct();
	}
	
	public function list(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["query",false,"姓名或手机号",'']
			],'post');
			$this->db->where("shopid",$token["shopid"]);
			$this->db->like("title",$data["query"]);
			$users=$this->db->get("timecard")->result_array();
			
			$this->WRITE("code",1);
			$this->WRITE("list",$users);
			$this->RESPONSE();
		}	
	}
	
	//根据会员ID获取名下可用计次卡
	public function list_by_uid(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["uid",true,"会员ID"]],'post');
			
			$this->db->where("uid",$data["uid"]);
			$this->db->where("count>0");
			$this->db->select("tcrid,ordercode,uid,username,shopid,timecardtitle,count,expdate,createtime,memo");
			$cards=$this->db->get("charge_timecard")->result_array();
		
			$this->WRITE("code",1);
			$this->WRITE("list",$cards);
			$this->RESPONSE();
		
		}
	}
	
	public function listop(){
		$token=$this->verifyToken();
		if($token){
			$this->db->where("shopid",$token["shopid"]);
			//$this->db->select("title,memo");
			$users=$this->db->get("timecard")->result_array();
			
			$this->WRITE("code",1);
			$this->WRITE("list",$users);
			$this->RESPONSE();
		}	
	}
	
	public function create(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["title",true,"次卡名称"],
			["count",true,"次数"],
			["sum",true,"次卡金额"],
			["memo",false,"备注信息",""]],'post');
			
			$data["shopid"]=$token["shopid"];
			$this->db->insert("timecard",$data);
			
			$this->WRITE("code",1);
			$this->WRITE("msg","保存成功");
			$this->RESPONSE();
		}
	}
	
	public function update(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["tcid",true,"次卡id"],
			["title",true,"次卡名称"],
			["count",true,"次数"],
			["sum",true,"次卡金额"],
			["memo",false,"备注信息",""]],'post');
			
			$data["shopid"]=$token["shopid"];
			$this->db->where("tcid",$data["tcid"]);
			$this->db->update("timecard",$data);
			
			$this->WRITE("code",1);
			$this->WRITE("msg","保存成功");
			$this->RESPONSE();
		}
	}
	
	public function remove(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["tcid",true,"次卡id"]],"post");
			
			$res=$this->db->where("tcid",$data["tcid"])->delete("timecard");
			if($res){
				$this->WRITE("code",1);
				$this->WRITE("msg","删除成功");
				$this->RESPONSE();
			}else{
				$this->WRITE("code",0);
				$this->WRITE("msg","删除失败");
				$this->RESPONSE();
			}
		}
	}

}
